<?php

class table {

    public $tablearr; //存放表名（或多表连接）的数组
    public $tablename, $field;
    public $search, $defaorder;
    public $pageinfo;
    public $hiddendel = 1; //隐藏删除的

    public function __construct($tablearr = array()) {
        $this->tablearr = $tablearr; //读入表名数组
    }

    public function getList() {
        $tablename = $tablename_first = '';
        if ($this->tablename) {
            $tablename_first = $this->tablename;
            $tablename = $this->tablearr[$this->tablename];
        } else {
            //开始分析$field里面的表
            preg_match_all("/(([a-z]+)\.)/", $this->field, $matches);
            $tablearr = array_unique($matches[2]);
            $tablename_first = $oldtname = array_shift($tablearr);
            $tablename = 'firphp_' . $oldtname . ' `' . $oldtname . '`';
            foreach ($tablearr as $tname) {
                $tablename.=' LEFT JOIN firphp_' . $tname . ' `' . $tname . '` ON `' . $tname . '`.id=`' . $oldtname . '`.' . $tname . 'id';
                $oldtname = $tname;
            }
        }
        $wheresql = "1";
        $i = 0;
        $wherearr = array();
        $str_delfield = $tablename_first . '.del_time';
        if ($this->hiddendel)
            $this->search[$str_delfield] = 0;
        foreach ($this->search as $key => $val) {
            //只有正确的搜索项才会被添加匹配
            if (preg_match("/^[a-z]{3,12}\.[a-z\_]{2,12}$/", $key) && $val !== '') {
                $wheresql.= " AND {$key}='{{$i}}'";
                $wherearr[] = $val;
                $i++;
            }
        }
        $db = db::getInstance();
        $field = $str_delfield . ',' . $this->field;
        //die($this->field);
        $ret = $db->table($tablename)->where($wheresql, $wherearr)->page($this->pageinfo)->field($field)->order($this->order, $this->defaorder)->select();
        return($ret);
    }

}
